package com.vincce.sport.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.ConvertUtil;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.CompetitionGroup;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.CompetitionTeam;
import com.vincce.sport.entity.GroupTaskEntity;
import com.vincce.sport.entity.PreliminaryContest;
import com.vincce.sport.entity.Video;
import com.vincce.sport.entity.Zone;
import com.vincce.sport.mapper.CompetitionGroupMapper;
import com.vincce.sport.mapper.CompetitionSeasonMapper;
import com.vincce.sport.mapper.CompetitionTeamMapper;
import com.vincce.sport.mapper.GroupTaskMapper;
import com.vincce.sport.mapper.MatchTaskMapper;
import com.vincce.sport.mapper.PreliminaryContestMapper;
import com.vincce.sport.mapper.SchoolMapper;
import com.vincce.sport.mapper.TeamDateMapper;
import com.vincce.sport.mapper.TeamMapper;
import com.vincce.sport.mapper.ZoneMapper;

@Controller
@RequestMapping("match")
public class MatchController {

	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private ZoneMapper zoneMapper;
	@Resource
	private SchoolMapper schoolMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private TeamDateMapper teamDateMapper;
	@Resource
	private PreliminaryContestMapper preliminaryContestMapper;
	@Resource
	private CompetitionGroupMapper competitionGroupMapper;
	@Resource
	private CompetitionTeamMapper competitionTeamMapper;
	@Resource
	private GroupTaskMapper groupTaskMapper;
	@Resource
	private MatchTaskMapper matchTaskMapper;
	
	@RequestMapping("show")
	public String show(HttpServletRequest request,Model model,String seasonId
			,String zoneId,String schoolId,String teamId,String start_time,String end_time){
		//获取赛季信息
		List<CompetitionSeason> seasonList = competitionSeasonMapper.querySeasonByAll();
		//获取赛区
		List<Zone> zoneList = zoneMapper.queryByZoneAll();
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("seasonId", seasonId);
		map.put("zoneId", zoneId);
		map.put("seasonId", seasonId);
		map.put("schoolId", schoolId);
		map.put("teamId", teamId);
		map.put("start_time", start_time);
		map.put("end_time", end_time);
		//首次访问页面，where 1=2
		if(CheckUtil.isEmpty(seasonId)&&CheckUtil.isEmpty(zoneId)&&CheckUtil.isEmpty(schoolId)&&
				CheckUtil.isEmpty(teamId)&&CheckUtil.isEmpty(start_time)&&CheckUtil.isEmpty(end_time)){
			map.put("cond", "1");
		}
		//预赛对阵情况
		//获取指定日期段内的有赛事的日期分组
		/*if(CheckUtil.isEmpty(start_time) && CheckUtil.isEmpty(end_time)){
			map.put("start_time", ConvertUtil.format("yyyy-MM-dd", new Date()));
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.getInstance().DATE, 3);
			Date end = calendar.getTime();
			map.put("end_time", ConvertUtil.format("yyyy-MM-dd", end));
		}*/
		/*List<PreliminaryContest> dateList = preliminaryContestMapper.queryDateGroupBy4Condition(map);
		//获取比赛详情
		List<PreliminaryContest> matchList = preliminaryContestMapper.queryPreliminaryBy4Condition(map);
		if(CheckUtil.isEmpty(seasonId)){
			map.put("cond", "no");
		}*/
		//从临时表中获取信息
		//获取日期分组
		List<PreliminaryContest> dateList = matchTaskMapper.queryDateGroup(map);
		List<PreliminaryContest> matchList = matchTaskMapper.findPreliminaryBy4Condition(map);
		//获取分组信息
		//map.clear();
		/*if(CheckUtil.isEmpty(map.get("seasonId"))){
			Long maxSeasonId =  groupTaskMapper.queryMaxSeasonId();
			map.put("maxSeasonId", maxSeasonId);
		}*/
		List<CompetitionGroup> groupList = competitionGroupMapper.findFrontCompetitionGroupList(map);
		List<GroupTaskEntity> teamInfoList  = groupTaskMapper.queryBySeasonId(map);
		/*//分组球队信息（小组中球队的序号、球队名字、球队ID）
		List<CompetitionTeam> teamInfoList = competitionTeamMapper.queryTeamInfo(map);
		//获取分组后球队比赛场数    根据球队id和 赛季id
		for (CompetitionTeam competitionTeam : teamInfoList) {
			Map teamMap = new HashMap();
			teamMap.put("seasonId", seasonId);
			teamMap.put("teamId", competitionTeam.getTeamId());
			//计算球队比赛场数
			competitionTeam.setComCount(teamDateMapper.queryTeamDateByCount(teamMap));
			//胜场
			teamMap.put("winOrLose", "win");
			competitionTeam.setWinCount(teamDateMapper.queryTeamDateByCount(teamMap));
			//败场
			teamMap.put("winOrLose", "lose");
			competitionTeam.setLoseCount(teamDateMapper.queryTeamDateByCount(teamMap));
			//计算积分
			competitionTeam.setIntegration(competitionTeam.getWinCount()*2+competitionTeam.getLoseCount()*1);
		}*/
		//根据seasonId查找淘汰赛图片
		CompetitionSeason eliminateImg = competitionSeasonMapper.findCompetitionSeasonById(Long.parseLong(seasonId == null?"0":seasonId));
		model.addAttribute("eliminateImg", eliminateImg == null ? eliminateImg : replace(eliminateImg));
		model.addAttribute("teamInfoList", teamInfoList);
		model.addAttribute("groupList", groupList);
		model.addAttribute("dateList", dateList);
		model.addAttribute("matchList", matchList);
		model.addAttribute("season", seasonList);
		model.addAttribute("zone", zoneList);
		//回显
		model.addAttribute("seasonId", seasonId);
		model.addAttribute("zoneId", zoneId);
		model.addAttribute("schoolId", schoolId);
		model.addAttribute("teamId", teamId);
		model.addAttribute("start_time", start_time);
		model.addAttribute("end_time", end_time);
		
		return "foreground/match";
	}
	
	//转换图片路径
	private CompetitionSeason replace(CompetitionSeason eliminateImg) {
				eliminateImg.setEliminateImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+eliminateImg.getEliminateImg(), "ori"));
				return eliminateImg;
			}
		
}
